package com.example.zhangmeng.java_web.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.example.zhangmeng.java_web.model.User;
import com.example.zhangmeng.java_web.repository.UserRepository;

@Controller
public class UserController {
    private final UserRepository userRepository;

    public UserController(UserRepository userRepository){
        this.userRepository = userRepository;
    }

    @GetMapping("/users")
    public String listUsers(Model model) {
        model.addAttribute("users", userRepository.findAll());
        return "user-list"; // 对应模板文件名
    }

    
    @PostMapping("/user")
    public ResponseEntity<String> submitData(@RequestParam String name, @RequestParam String email) {
        // 处理业务逻辑（如保存到数据库）
        User user = new User();
        user.setName(name);
        user.setEmail(email);
        userRepository.insertOne(user);
        return ResponseEntity.ok("数据已接收");
    }
}
